package com.hdax.dm.controller;

import cn.hutool.core.date.DateUtil;
import com.hdax.dm.beans.pay.trade;
import com.hdax.dm.feign.payFeign;
import com.hdax.dm.service.queryorderlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrderPayController {

    @Autowired
    private queryorderlistService queryorderlistService;

    @Autowired
    private payFeign payFeign;

//    @GetMapping("payed")
//    public void getchangeOrder(HttpServletRequest request){
//
//        //支付成功后跳转到此处
//        //进行修改订单状态
//        String tradeNo = request.getParameter("out_trade_no");
//        queryorderlistService.succeedupdate(tradeNo);
//
//        System.out.println("支付宝订单号："+request.getParameter("trade_no"));
//        System.out.println("商家订单号："+request.getParameter("out_trade_no"));
//        System.out.println("买家支付宝id："+request.getParameter("buyer_id"));
//        System.out.println("交易状态："+request.getParameter("trade_status"));
//        System.out.println("实收金额："+request.getParameter("receipt_amount"));
//        System.out.println("用户支付金额："+request.getParameter("buyer_pay_amount"));
//        System.out.println("交易付款时间："+request.getParameter("gmt_payment"));
//
//        trade trade = new trade();
//        trade.setId(request.getParameter("trade_no"));
//        trade.setTradeNo(request.getParameter("trade_no"));
//        trade.setOrderNo(request.getParameter("out_trade_no"));
//        trade.setPayMethod(1L);
//        trade.setAmount(Double.parseDouble(request.getParameter("receipt_amount")));
//        trade.setCreatedTime(request.getParameter("gmt_payment"));
//
//        payFeign.addpay(trade);
//    }

    @PostMapping("payed")
    public void postchangeOrder(HttpServletRequest request){

        //支付成功后跳转到此处
        //进行修改订单状态
        String tradeNo = request.getParameter("out_trade_no");
        queryorderlistService.succeedupdate(tradeNo);

        System.out.println("支付宝订单号："+request.getParameter("trade_no"));
        System.out.println("商家订单号："+request.getParameter("out_trade_no"));
        System.out.println("买家支付宝id："+request.getParameter("buyer_id"));
        System.out.println("交易状态："+request.getParameter("trade_status"));
        System.out.println("实收金额："+request.getParameter("receipt_amount"));
        System.out.println("用户支付金额："+request.getParameter("buyer_pay_amount"));
        System.out.println("交易付款时间："+request.getParameter("gmt_payment"));

        trade trade = new trade();
        trade.setId(request.getParameter("trade_no"));
        trade.setTradeNo(request.getParameter("trade_no"));
        trade.setOrderNo(request.getParameter("out_trade_no"));
        trade.setPayMethod(1L);
        trade.setAmount(Double.parseDouble(request.getParameter("receipt_amount")));
        trade.setCreatedTime(request.getParameter("gmt_payment"));

        payFeign.addpay(trade);
    }
}
